<?php

class ml_biz_atmeOutput
{
    const PAGESIZE = 40;
    private $_html;
    private $_nicks;
    private $_contentlist;
    private $_feedlist;
    private $_total;
    
    
    public function main($uid , $blocknum = 0)
    {
        $oAtme = new ml_model_dbAtMe();
        $rs = $oAtme->getTotalByUid($uid);
        $this->_total = $oAtme->get_data();
        
        
        $rs = $oAtme->getListByUid($uid , $blocknum , self::PAGESIZE );
        if(!$rs)
            return false;
            
        $aRows = $oAtme->get_data();
        if(count($aRows) > 0)
        {
            $aRid  = $aRidAll = array();
            
            foreach ($aRows as &$row)
            {
                $aUid[] = $row['act_uid'];
                switch ($row['type'])
                {
                    case ML_ATME_TYPE_IN_CONTENT:
                        $type = ml_tool_resid::resid2type($row['atme_info']['act_rid']);
                        $row['res_type'] = $type;
                        $aRid[$type][] = $row['atme_info']['act_rid'];
                        $aRidAll[] = $row['atme_info']['act_rid'];
                        $aUid[] = ml_tool_resid::resid2uid($row['atme_info']['act_rid']);
                        break;
                    case ML_ATME_TYPE_IN_COLLECTION:
                        $type = ml_tool_resid::resid2type($row['atme_info']['dest_rid']);
                        $row['res_type'] = $type;
                        $aRid[$type][] = $row['atme_info']['dest_rid'];
                        $aRidAll[] = $row['atme_info']['dest_rid'];
                        $aUid[] = ml_tool_resid::resid2uid($row['atme_info']['dest_rid']);
                        break;
                    case ML_ATME_TYPE_IN_NEWCOMMENT:
                    case ML_ATME_TYPE_IN_REPLYCOMMENT:
                        $type = ml_tool_resid::resid2type($row['atme_info']['src_rid']);
                        $row['res_type'] = $type;
                        $aRid[$type][] = $row['atme_info']['src_rid'];
                        $aRidAll[] = $row['atme_info']['src_rid'];
                        $aUid[] = ml_tool_resid::resid2uid($row['atme_info']['src_rid']);
                        break;
                    default:
                        break;
                }
                
            }
            
            //
            if(isset($aRid[ML_RESID_TYPE_CONTENT]) && count($aRid[ML_RESID_TYPE_CONTENT]))
                $this->_fetchContent($aRid[ML_RESID_TYPE_CONTENT]);
            if(isset($aRid[ML_RESID_TYPE_USERFEED]) && count($aRid[ML_RESID_TYPE_USERFEED]))
                $this->_fetchUserfeed($aRid[ML_RESID_TYPE_USERFEED]);
            /**
             * @todo 取数字
             */
            
            $this->_nicks = ml_tool_nick::uids2nicks($aUid);
            
            foreach ($aRows as &$row)
            {
                switch ($row['type'])
                {
                    case ML_ATME_TYPE_IN_CONTENT:
                        $row = $this->_format_in_content($row);break;
                    case ML_ATME_TYPE_IN_COLLECTION:
                        $row = $this->_format_in_collect($row);break;
                    case ML_ATME_TYPE_IN_NEWCOMMENT:
                        $row = $this->_format_in_comment($row);break;
                    case ML_ATME_TYPE_IN_REPLYCOMMENT:
                        $row = $this->_format_in_comment_reply($row);break;    
                    default:break;
                }
                                
                                
                $row['user'] = self::_format_user($row['act_uid'] , $this->_nicks[$row['act_uid']]);
                $row['content'] = ml_tool_formatContent::format_content($row['content']);
                
                if($row['res']['gd_price'])
                {
                    $row['goods']['price'] = ml_tool_standardShow::rmb_price($row['res']['gd_price']);
                    $row['goods']['title'] = Tool_string::un_html($row['res']['gd_title']);
                }
                if($row['res']['pic_id'])
                {
                    $row['pic_url'] = ml_tool_picid::pid2url($row['res']['pic_id'] , ML_USER_PICSIZE_FEED , ML_PICTYPE_CONTENT);
                    $row['big_pic_url'] = ml_tool_picid::pid2url($row['res']['pic_id'] , ML_USER_PICSIZE_SHOW , ML_PICTYPE_CONTENT);
                }
                $row['ctime'] = ml_tool_standardShow::time($row['ctime']);
                if($row['res_type'] == ML_RESID_TYPE_CONTENT)
                    $row['goods_url'] = ml_tool_urlMaker::show_goods($row['rid']);
                
                    
                $row['res_content'] = ml_tool_formatContent::format_content($row['res']['content']);
                $row['res_user'] = self::_format_user($row['res']['uid'] , $this->_nicks[$row['res']['uid']]);
            }
            $this->_html = $this->_render_html($aRows);
            return true;
        }
        else 
        {
            $this->_html = array();
            return true;
        }
        
    }
    public function get_html()
    {
        return $this->_html;
    }
    public function get_total()
    {
        return $this->_total;
    }
    
    private function _fetchContent($aRid)
    {
        $oContent = new ml_model_dbContent();
        $this->_contentlist = $oContent->get_content_by_rids($aRid);
        
        
        return true;
    }
    private function _fetchUserfeed($aRid)
    {
        $oUserfeed = new ml_model_dbUserfeed();
        $this->_feedlist = $oUserfeed->getUserfeedByRids($aRid);
        
        return true;
    }
    
    private function _format_in_content($row)
    {
        $row['show_type'] = $row['res_type'] == ML_RESID_TYPE_CONTENT ? 'pic' : 'txt';
        $row['rid'] = $row['atme_info']['act_rid'];
        $row['res_type'] = ml_tool_resid::resid2type($row['rid']);
        $row['res'] = $row['res_type'] == ML_RESID_TYPE_CONTENT 
                        ? $this->_contentlist[$row['rid']] 
                        : $this->_feedlist[$row['rid']];
        $row['content'] = $row['res']['content'];
        return $row;
    }
    private function _format_in_collect($row)
    {
        $row['show_type'] = 'quote';
        $row['rid'] = $row['atme_info']['dest_rid'];
        $row['res_type'] = ml_tool_resid::resid2type($row['rid']);
        $row['res'] = $this->_contentlist[$row['rid']];
        $row['content'] = '转采了你的宝贝';
        return $row;
    }
    private function _format_in_comment($row)
    {
        $row['show_type'] = 'quote';
        $row['rid'] = $row['atme_info']['src_rid'];
        $row['res_type'] = ml_tool_resid::resid2type($row['rid']);
        $row['res'] = $row['res_type'] == ML_RESID_TYPE_CONTENT 
                        ? $this->_contentlist[$row['rid']] 
                        : $this->_feedlist[$row['rid']];
        $row['cmt_id'] = ml_tool_resid::resid2id($row['atme_info']['cmt_rid']);
        return $row;
    }
    private function _format_in_comment_reply($row)
    {
        $row['show_type'] = 'quote';
        $row['rid'] = $row['atme_info']['src_rid'];
        $row['res_type'] = ml_tool_resid::resid2type($row['rid']);
        $row['res'] = $row['res_type'] == ML_RESID_TYPE_CONTENT 
                        ? $this->_contentlist[$row['rid']] 
                        : $this->_feedlist[$row['rid']];
        $row['cmt_id'] = ml_tool_resid::resid2id($row['atme_info']['cmt_rid']);
        return $row;
    }
    
    private function _format_user($uid , $nick)
    {
        return  array(
                'uid' => $uid,
                'nick' => Tool_string::un_html($nick),
                'homeurl' => ml_tool_urlMaker::person_index($uid),
                'portrait' => ml_tool_picid::uid2portrait($uid)
            );
    }
    
    private function _render_html($rows)
    {
        foreach ($rows as $row)
        {
            $data = array(
                'canComment' => true,
                'row' => $row
            );
            switch ($row['show_type'])
            {
                
                
                case 'pic':
                    $html[] = ml_tool_template::parseModTpl($data , '/pub_feed::txtpic');break;
                case 'txt':
                    $html[] = ml_tool_template::parseModTpl($data , '/pub_feed::txtonly');break;
                case 'quote':
                    $data['is_del']=empty($data['row']['res'])?true:false;
                    $html[] = ml_tool_template::parseModTpl($data , '/pub_feed::txtquote');break;
            }
        }
        return $html;
    }
}